import { defineStore } from 'pinia';

export const useMultimeterStore = defineStore('multimeter', {
  state: () => ({
    // 当前测量模式
    currentMode: 'voltage',
    
    // 当前测量范围
    currentRange: '20V',
    
    // 电源状态
    isPoweredOn: true,
    
    // 数据保持状态
    isHoldActive: false,
    
    // 背光状态
    backlightEnabled: false,
    
    // 选中的端口
    selectedPort: 'com',
    
    // 测量模式定义
    measurementModes: [
      { value: "off", label: "关闭", unit: "", rotation: 270 },
      { value: "voltage", label: "电压", unit: "V", rotation: 0 },
      { value: "resistance", label: "电阻", unit: "Ω", rotation: 90 },
      { value: "current", label: "电流", unit: "A", rotation: 180 },
    ],
    
    // 测量范围定义
    measurementRanges: {
      voltage: [
        { value: "200mV", label: "200mV", max: 0.2 },
        { value: "2V", label: "2V", max: 2 },
        { value: "20V", label: "20V", max: 20 },
        { value: "200V", label: "200V", max: 200 },
        { value: "1000V", label: "1000V", max: 1000 },
      ],
      current: [
        { value: "200μA", label: "200μA", max: 0.0002 },
        { value: "2mA", label: "2mA", max: 0.002 },
        { value: "20mA", label: "20mA", max: 0.02 },
        { value: "200mA", label: "200mA", max: 0.2 },
        { value: "20A", label: "20A", max: 20 },
      ],
      resistance: [
        { value: "200Ω", label: "200Ω", max: 200 },
        { value: "2kΩ", label: "2kΩ", max: 2000 },
        { value: "20kΩ", label: "20kΩ", max: 20000 },
        { value: "200kΩ", label: "200kΩ", max: 200000 },
        { value: "2MΩ", label: "2MΩ", max: 2000000 },
        { value: "20MΩ", label: "20MΩ", max: 20000000 },
      ],
    }
  }),
  
  getters: {
    // 获取当前测量模式
    getCurrentMode: (state) => state.currentMode,
    
    // 获取当前测量范围
    getCurrentRange: (state) => state.currentRange,
    
    // 获取电源状态
    getIsPoweredOn: (state) => state.isPoweredOn,
    
    // 获取数据保持状态
    getIsHoldActive: (state) => state.isHoldActive,
    
    // 获取背光状态
    getBacklightEnabled: (state) => state.backlightEnabled,
    
    // 获取选中的端口
    getSelectedPort: (state) => state.selectedPort,
    
    // 获取所有测量模式
    getMeasurementModes: (state) => state.measurementModes,
    
    // 获取所有测量范围
    getMeasurementRanges: (state) => state.measurementRanges,
    
    // 获取当前模式的测量范围
    getCurrentModeRanges: (state) => {
      return state.measurementRanges[state.currentMode] || [];
    },
    
    // 获取当前单位
    getCurrentUnit: (state) => {
      if (!state.isPoweredOn || state.currentMode === "off") return "";

      // 根据当前范围调整单位显示
      if (state.currentMode === "voltage") {
        if (state.currentRange.includes("mV")) return "mV";
        return "V";
      } else if (state.currentMode === "current") {
        if (state.currentRange.includes("μA")) return "μA";
        if (state.currentRange.includes("mA")) return "mA";
        return "A";
      } else if (state.currentMode === "resistance") {
        if (state.currentRange.includes("kΩ")) return "kΩ";
        if (state.currentRange.includes("MΩ")) return "MΩ";
        return "Ω";
      }
      return "";
    },
    
    // 获取旋钮旋转角度
    getKnobRotation: (state) => {
      const mode = state.measurementModes.find((m) => m.value === state.currentMode);
      return mode ? mode.rotation : 0;
    }
  },
  
  actions: {
    // 设置当前测量模式
    setCurrentMode(mode) {
      this.currentMode = mode;
      
      // 模式切换时重置范围到默认值
      if (mode === "voltage") {
        this.currentRange = "20V";
      } else if (mode === "resistance") {
        this.currentRange = "200Ω";
      } else if (mode === "current") {
        this.currentRange = "20mA";
      }
    },
    
    // 设置当前测量范围
    setCurrentRange(range) {
      this.currentRange = range;
    },
    
    // 设置电源状态
    setIsPoweredOn(status) {
      this.isPoweredOn = status;
      
      // 关机时自动关闭背光
      if (!status) {
        this.backlightEnabled = false;
      }
    },
    
    // 设置数据保持状态
    setIsHoldActive(status) {
      this.isHoldActive = status;
    },
    
    // 设置背光状态
    setBacklightEnabled(status) {
      this.backlightEnabled = status;
    },
    
    // 设置选中的端口
    setSelectedPort(port) {
      this.selectedPort = port;
    },
    
    // 重置状态
    reset() {
      this.currentMode = 'voltage';
      this.currentRange = '20V';
      this.isPoweredOn = true;
      this.isHoldActive = false;
      this.backlightEnabled = false;
      this.selectedPort = 'com';
    }
  }
});